package cn.mandifal.user.service.impl;
import java.util.Date;
import java.util.List;

import cn.mandifal.mapper.DpAddressMapper;
import cn.mandifal.mapper.DpAreasMapper;
import cn.mandifal.mapper.DpCitiesMapper;
import cn.mandifal.mapper.DpProvincesMapper;
import cn.mandifal.pojo.*;
import cn.mandifal.user.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import org.springframework.cglib.transform.impl.AddDelegateTransformer;

/**
 * 服务实现层
 * @author shun
 *
 */
@Service
public class AddressServiceImpl implements AddressService {

	@Autowired
	private DpAddressMapper addressMapper;
	@Autowired
	private DpProvincesMapper dpProvincesMapper;
	@Autowired
	private DpCitiesMapper dpCitiesMapper;
	@Autowired
	private DpAreasMapper dpAreasMapper;

	/**
	 * 增加
	 */
	@Override
	public void add(DpAddress address) {
		//转换地址的省市区编号 为对应的名字
		DpAddress dpAddress = conversionToName(address);
		//设置创建时间
		dpAddress.setCreateDate(new Date());
		//2.根据用户名查询地址表，如果没有该用户的记录则设置为默认地址，如果存在记录，则不设置
		DpAddressExample example=new DpAddressExample();
		DpAddressExample.Criteria criteria = example.createCriteria();
		//根据用户名查询地址
		criteria.andUserIdEqualTo(dpAddress.getUserId());
		List<DpAddress> dpAddresses = addressMapper.selectByExample(example);
		if (dpAddresses==null){
			//不存在该用的记录则设置此地址为默认地址
			dpAddress.setIsDefault("1");
		}else{
			//设置不默认,0
			dpAddress.setIsDefault("0");
		}
		addressMapper.insert(dpAddress);
	}

	/**
	 * 转换地址的省市区编号 为对应的名字
	 * @param address
	 * @return
	 */
	private DpAddress conversionToName(DpAddress address){
		//1.得到前端传来的地址的 id，查出所对应的省市区，重新设置这三个地址字段
		//通过省编号查询省名字
		DpProvinces dpProvinces = dpProvincesMapper.selectByPrimaryKey(address.getProvinceId());
		//重新设置地址表的省字段
		address.setProvinceId(dpProvinces.getProvince());
		System.out.println(dpProvinces.getProvince());
		//得到市编号,查询市名字，重新设置地址表市字段
		address.setCityId(dpCitiesMapper.selectByPrimaryKey(address.getCityId()).getCity());
		//得到区编号，查询区名字，重新设置地址表的区字段
		address.setTownId(dpAreasMapper.selectByPrimaryKey(address.getTownId()).getArea());
		return address;
	}
	/**
	 * 修改
	 */
	@Override
	public void update(DpAddress address){
		//转换地址的省市区编号 为对应的名字
		DpAddress dpAddress = conversionToName(address);
		//保存
		addressMapper.updateByPrimaryKey(dpAddress);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public DpAddress findOne(Long id){
		//根据 id 得到地址
		DpAddress dpAddress = addressMapper.selectByPrimaryKey(id);
		//根据地址中存的省市区的名字得到相对应的编号
		conversionToNumber(dpAddress);
		return dpAddress;
	}

	/**
	 * 转换省市区名字到对应编号
	 * @param dpAddress
	 */
	private DpAddress conversionToNumber(DpAddress dpAddress) {
		//1.得到省名字,转换成对应的编号
		DpProvincesExample provinceExample=new DpProvincesExample();
		DpProvincesExample.Criteria criteria = provinceExample.createCriteria();
		criteria.andProvinceEqualTo(dpAddress.getProvinceId());
		List<DpProvinces> dpProvinces = dpProvincesMapper.selectByExample(provinceExample);
		String provinceid = dpProvinces.get(0).getProvinceid();
		//设置省级编号
		dpAddress.setProvinceId(provinceid);
		//2.得到市名字，并根据省名字和市名字转换成相应编号
		DpCitiesExample dpCitiesExample=new DpCitiesExample();
		DpCitiesExample.Criteria dpCitiesExampleCriteria = dpCitiesExample.createCriteria();
		dpCitiesExampleCriteria.andProvinceidEqualTo(provinceid);
		dpCitiesExampleCriteria.andCityEqualTo(dpAddress.getCityId());
		List<DpCities> dpCities = dpCitiesMapper.selectByExample(dpCitiesExample);
		String cityid = dpCities.get(0).getCityid();
		//设置市级编号
		dpAddress.setCityId(cityid);
		//3.根据市级编号和区名字得到区编号
		DpAreasExample dpAreasExample=new DpAreasExample();
		DpAreasExample.Criteria dpAreasExampleCriteria = dpAreasExample.createCriteria();
		dpAreasExampleCriteria.andCityidEqualTo(cityid);
		dpAreasExampleCriteria.andAreaEqualTo(dpAddress.getTownId());
		List<DpAreas> dpAreas = dpAreasMapper.selectByExample(dpAreasExample);
		dpAddress.setTownId(dpAreas.get(0).getAreaid());
		return dpAddress;
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long id) {
			addressMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 查询所有的省级地址
	 * @return
	 */
    @Override
    public List<DpProvinces> findProvinces() {
    	//条件传入 null 表示查询所有
		List<DpProvinces> dpProvincesList = dpProvincesMapper.selectByExample(null);
		return dpProvincesList;
    }

	/**
	 * 根据省级编号查询市级列表
	 * @param provinceId 省级编号
	 * @return
	 */
	@Override
	public List<DpCities> findCity(String provinceId) {
		//通过省表 id 查询到省级编号
		DpProvinces dpProvinces = dpProvincesMapper.selectByPrimaryKey(provinceId);
		DpCitiesExample example=new DpCitiesExample();
		DpCitiesExample.Criteria criteria = example.createCriteria();
		//传入省级编号
		criteria.andProvinceidEqualTo(dpProvinces.getProvinceid());
		List<DpCities> dpCities = dpCitiesMapper.selectByExample(example);
		return dpCities;
	}

	/**
	 * 根据市级编号查询县级列表
	 * @param cityId 市级编号
	 * @return
	 */
	@Override
	public List<DpAreas> findArea(String cityId) {
		//根据市级 id 查询到市级编号
		DpCities dpCities = dpCitiesMapper.selectByPrimaryKey(cityId);
		DpAreasExample example=new DpAreasExample();
		DpAreasExample.Criteria criteria = example.createCriteria();
		criteria.andCityidEqualTo(dpCities.getCityid());
		List<DpAreas> dpAreas = dpAreasMapper.selectByExample(example);
		return dpAreas;
	}

	/**
	 * 通过用户名来查找收货地址
	 * @param userId 用户名
	 * @return 返回结果
	 */
	@Override
	public List<DpAddress> findListByUserId(String userId) {

		DpAddressExample example=new DpAddressExample();
		DpAddressExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		return addressMapper.selectByExample(example);
	}

	/**
	 * 设置默认地址
	 * @param id 地址 id
	 */
	@Override
	public void isDefault(Long id,String username) {
		//1.根据用户名得到此用户的旧默认地址
		DpAddressExample example=new DpAddressExample();
		DpAddressExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(username);
		criteria.andIsDefaultEqualTo("1");
		List<DpAddress> dpAddresses = addressMapper.selectByExample(example);
		//其实只有一个是默认地址，但还是按照有多个来写逻辑
		//设置就默认地址的字段为0
		for (DpAddress dpAddress:dpAddresses) {
			dpAddress.setIsDefault("0");
			//保存
			addressMapper.updateByPrimaryKey(dpAddress);
		}
		//2.通过 id 来设置新的默认地址
		DpAddress dpAddress = addressMapper.selectByPrimaryKey(id);
		dpAddress.setIsDefault("1");
		//保存
		addressMapper.updateByPrimaryKey(dpAddress);
	}

}
